package com.ruoyi.report_ljlm.service.impl;

import cn.hutool.core.bean.BeanUtil;
import cn.hutool.core.collection.CollUtil;
import com.baomidou.dynamic.datasource.annotation.DSTransactional;
import com.ruoyi.common.utils.StringUtils;
import com.ruoyi.common.core.page.TableDataInfo;
import com.ruoyi.common.core.domain.PageQuery;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.ruoyi.report_ljlm.domain.PzdjlbBsDetail;
import com.ruoyi.report_ljlm.domain.vo.PzdjlbBsDetailVo;
import com.ruoyi.report_ljlm.mapper.PzdjlbBsDetailMapper;
import com.ruoyi.syrw.annotation.FIllBasicInfo;
import lombok.RequiredArgsConstructor;
import org.springframework.stereotype.Service;
import com.ruoyi.report_ljlm.domain.bo.PzdjlbBsInfoBo;
import com.ruoyi.report_ljlm.domain.vo.PzdjlbBsInfoVo;
import com.ruoyi.report_ljlm.domain.PzdjlbBsInfo;
import com.ruoyi.report_ljlm.mapper.PzdjlbBsInfoMapper;
import com.ruoyi.report_ljlm.service.IPzdjlbBsInfoService;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;
import java.util.Collection;

/**
 * 路基路面平整度试验检测记录(连续式平整度仪)Service业务层处理
 *
 * @author llb
 * @date 2024-07-10
 */
@RequiredArgsConstructor
@Service
public class PzdjlbBsInfoServiceImpl implements IPzdjlbBsInfoService {

    private final PzdjlbBsInfoMapper baseMapper;
    private final PzdjlbBsDetailMapper detailMapper;

    /**
     * 查询路基路面平整度试验检测记录(连续式平整度仪)
     */
    @Override
    @FIllBasicInfo
    public PzdjlbBsInfoVo queryById(Long excelId){
        PzdjlbBsInfoVo pzdjlbBsInfoVo = baseMapper.selectVoById(excelId);
        List<PzdjlbBsDetailVo> detailList = detailMapper.selectVoList(
            new LambdaQueryWrapper<PzdjlbBsDetail>().eq(PzdjlbBsDetail::getExcelId, excelId));
        if (CollUtil.isNotEmpty(detailList)) {
            pzdjlbBsInfoVo.setDetailList(detailList);
        }
        return pzdjlbBsInfoVo;
    }

    /**
     * 查询路基路面平整度试验检测记录(连续式平整度仪)列表
     */
    @Override
    public TableDataInfo<PzdjlbBsInfoVo> queryPageList(PzdjlbBsInfoBo bo, PageQuery pageQuery) {
        LambdaQueryWrapper<PzdjlbBsInfo> lqw = buildQueryWrapper(bo);
        Page<PzdjlbBsInfoVo> result = baseMapper.selectVoPage(pageQuery.build(), lqw);
        return TableDataInfo.build(result);
    }

    /**
     * 查询路基路面平整度试验检测记录(连续式平整度仪)列表
     */
    @Override
    public List<PzdjlbBsInfoVo> queryList(PzdjlbBsInfoBo bo) {
        LambdaQueryWrapper<PzdjlbBsInfo> lqw = buildQueryWrapper(bo);
        return baseMapper.selectVoList(lqw);
    }

    private LambdaQueryWrapper<PzdjlbBsInfo> buildQueryWrapper(PzdjlbBsInfoBo bo) {
        Map<String, Object> params = bo.getParams();
        LambdaQueryWrapper<PzdjlbBsInfo> lqw = Wrappers.lambdaQuery();
        lqw.eq(bo.getTotalPage() != null, PzdjlbBsInfo::getTotalPage, bo.getTotalPage());
        lqw.eq(bo.getPage() != null, PzdjlbBsInfo::getPage, bo.getPage());
        lqw.eq(StringUtils.isNotBlank(bo.getJiancdwmc()), PzdjlbBsInfo::getJiancdwmc, bo.getJiancdwmc());
        lqw.eq(StringUtils.isNotBlank(bo.getJilbh()), PzdjlbBsInfo::getJilbh, bo.getJilbh());
        lqw.eq(StringUtils.isNotBlank(bo.getGongcmc()), PzdjlbBsInfo::getGongcmc, bo.getGongcmc());
        lqw.eq(StringUtils.isNotBlank(bo.getGongcbwyt()), PzdjlbBsInfo::getGongcbwyt, bo.getGongcbwyt());
        lqw.eq(StringUtils.isNotBlank(bo.getYangpmc()), PzdjlbBsInfo::getYangpmc, bo.getYangpmc());
        lqw.eq(StringUtils.isNotBlank(bo.getYangpbh()), PzdjlbBsInfo::getYangpbh, bo.getYangpbh());
        lqw.eq(StringUtils.isNotBlank(bo.getYangpsl()), PzdjlbBsInfo::getYangpsl, bo.getYangpsl());
        lqw.eq(StringUtils.isNotBlank(bo.getYangpzt()), PzdjlbBsInfo::getYangpzt, bo.getYangpzt());
        lqw.eq(StringUtils.isNotBlank(bo.getYangpsj()), PzdjlbBsInfo::getYangpsj, bo.getYangpsj());
        lqw.eq(StringUtils.isNotBlank(bo.getShiyjcrq()), PzdjlbBsInfo::getShiyjcrq, bo.getShiyjcrq());
        lqw.eq(StringUtils.isNotBlank(bo.getShiytjwd()), PzdjlbBsInfo::getShiytjwd, bo.getShiytjwd());
        lqw.eq(StringUtils.isNotBlank(bo.getShiytjsd()), PzdjlbBsInfo::getShiytjsd, bo.getShiytjsd());
        lqw.eq(StringUtils.isNotBlank(bo.getJiancyj()), PzdjlbBsInfo::getJiancyj, bo.getJiancyj());
        lqw.eq(StringUtils.isNotBlank(bo.getPandyj()), PzdjlbBsInfo::getPandyj, bo.getPandyj());
        lqw.eq(StringUtils.isNotBlank(bo.getZhuyyqsbmcjbh()), PzdjlbBsInfo::getZhuyyqsbmcjbh, bo.getZhuyyqsbmcjbh());
        lqw.eq(bo.getShejz() != null, PzdjlbBsInfo::getShejz, bo.getShejz());
        lqw.eq(bo.getQvjs() != null, PzdjlbBsInfo::getQvjs, bo.getQvjs());
        lqw.eq(bo.getHegl() != null, PzdjlbBsInfo::getHegl, bo.getHegl());
        lqw.eq(bo.getPingjz() != null, PzdjlbBsInfo::getPingjz, bo.getPingjz());
        lqw.eq(bo.getBiaozc() != null, PzdjlbBsInfo::getBiaozc, bo.getBiaozc());
        lqw.eq(bo.getBianyxs() != null, PzdjlbBsInfo::getBianyxs, bo.getBianyxs());
        lqw.eq(StringUtils.isNotBlank(bo.getFujsm()), PzdjlbBsInfo::getFujsm, bo.getFujsm());
        lqw.eq(StringUtils.isNotBlank(bo.getJianc()), PzdjlbBsInfo::getJianc, bo.getJianc());
        lqw.eq(StringUtils.isNotBlank(bo.getJil()), PzdjlbBsInfo::getJil, bo.getJil());
        lqw.eq(StringUtils.isNotBlank(bo.getFuh()), PzdjlbBsInfo::getFuh, bo.getFuh());
        lqw.eq(StringUtils.isNotBlank(bo.getRiq()), PzdjlbBsInfo::getRiq, bo.getRiq());
        lqw.eq(StringUtils.isNotBlank(bo.getShebIds()), PzdjlbBsInfo::getShebIds, bo.getShebIds());
        lqw.eq(StringUtils.isNotBlank(bo.getPandyjIds()), PzdjlbBsInfo::getPandyjIds, bo.getPandyjIds());
        lqw.eq(StringUtils.isNotBlank(bo.getJiancyjIds()), PzdjlbBsInfo::getJiancyjIds, bo.getJiancyjIds());
        return lqw;
    }

    /**
     * 新增路基路面平整度试验检测记录(连续式平整度仪)
     */
    @Override
    @DSTransactional
    public Boolean insertByBo(PzdjlbBsInfoBo bo) {
        PzdjlbBsInfo add = BeanUtil.toBean(bo, PzdjlbBsInfo.class);
        validEntityBeforeSave(add);
        boolean flag = baseMapper.insert(add) > 0;
        if (flag) {
            bo.setExcelId(add.getExcelId());
            ArrayList<PzdjlbBsDetail> details = new ArrayList<>(21);
            for (int i=1;i<=21;i++ ){
                details.add(new PzdjlbBsDetail(bo.getExcelId()));
            }
            detailMapper.insertBatch(details);
        }
        return flag;
    }

    /**
     * 修改路基路面平整度试验检测记录(连续式平整度仪)
     */
    @Override
    @DSTransactional
    public Boolean updateByBo(PzdjlbBsInfoBo bo) {
        PzdjlbBsInfo update = BeanUtil.toBean(bo, PzdjlbBsInfo.class);
        validEntityBeforeSave(update);
        boolean flag = baseMapper.updateById(update) > 0;
        if (flag) {
            detailMapper.updateBatchById(BeanUtil.copyToList(bo.getDetailList(), PzdjlbBsDetail.class));
        }
        return flag;
    }

    /**
     * 保存前的数据校验
     */
    private void validEntityBeforeSave(PzdjlbBsInfo entity){
        //TODO 做一些数据校验,如唯一约束
    }

    /**
     * 批量删除路基路面平整度试验检测记录(连续式平整度仪)
     */
    @Override
    public Boolean deleteWithValidByIds(Collection<Long> ids, Boolean isValid) {
        if(isValid){
            //TODO 做一些业务上的校验,判断是否需要校验
        }
        return baseMapper.deleteBatchIds(ids) > 0;
    }
}
